//
//  WBUserInfor.swift
//  weibo
//
//  Created by  郭航 on 16/6/26.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit

class WBUserInfor: NSObject {
    
    static let shareWBUserInfor: WBUserInfor = WBUserInfor()
    
    var userInforModel: WBUserInforModel?
    
    var access_token: String? {
        if userInforModel?.access_token == nil {
            return nil
        } else {
            if userInforModel?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userInforModel?.access_token
            } else {
                return nil
            }
        }
    }
    
    var isLogin: Bool {
        return access_token != nil
    }
    
    let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userInfor.data")
    
    
    override init() {
        super.init()
        userInforModel = getUserInfor()
    }
    
    func saveUserInfor(userInforModel: WBUserInforModel) -> Void {
        self.userInforModel = userInforModel
        
        NSKeyedArchiver.archiveRootObject(userInforModel, toFile: path)
    }
    
    func getUserInfor() -> WBUserInforModel? {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? WBUserInforModel
    }
    
}
